package com.ly.fn.biz.ins.srv.api.config;

import com.google.common.base.Predicates;
import com.ly.fn.inf.config.ctx.conf.SystemEnv;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author xiaoyuan.wang
 * @Date 2021/9/27 11:44
 * @Version 1.0
 */
@Configuration
@EnableSwagger2
@ComponentScan
public class ApiPluginAutoConfig {

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new GlobalCorsConfig();
    }

    @Bean
    public ResponseHandler responseHandler() {
        return new ResponseHandler();
    }

    @Bean
    public Docket createRestApi() {
        boolean enabled = true;

        if (!"integration".equals(SystemEnv.getEnv()) &&
                !"dev".equals(SystemEnv.getEnv()) &&
                !"stable".equals(SystemEnv.getEnv())) {
            enabled = false;
        }

        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enabled)
                .apiInfo(apiInfo())
                .select()
                .paths(PathSelectors.any())
                //不显示错误的接口地址
                .paths(Predicates.not(PathSelectors.regex("/error.*")))//错误路径不监控
                .paths(Predicates.not(PathSelectors.regex("/xpro.*")))
                .paths(Predicates.not(PathSelectors.regex("/invoke.*")))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("个金-" + SystemEnv.getSystemId() +" API代码自动生成")
                .description("个金Team")
                .version("1.0")
                .build();
    }
}
